package com.songshu.e.dubbo.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.songshu.e.dubbo.server.pojo.HouseResources;
import com.songshu.e.dubbo.server.service.BaseServiceImpl;
import com.songshu.e.dubbo.server.service.HouseResourcesService;
import com.songshu.e.dubbo.server.vo.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service  //Spring 服务
@Transactional //开启事务
public class HouseResourcesServiceImpl extends BaseServiceImpl<HouseResources> implements HouseResourcesService {
    @Override
    public int saveHouseResources(HouseResources houseResources) {
        //如果房源的标题为空 返回 -1
        if (StringUtils.isBlank(houseResources.getTitle())){
            return -1;
        }
        return super.save(houseResources);
    }

    @Override
    public PageInfo<HouseResources> queryHouseResourcesList(int page, int pageSize, HouseResources queryCondition) {
        QueryWrapper<HouseResources>  queryWrapper = new QueryWrapper<>(queryCondition);
        //按更新日期排列
        queryWrapper.orderByDesc("updated");
        IPage<HouseResources> iPage = super.queryPageList(queryWrapper,page,pageSize);

        return new PageInfo<HouseResources>(Long.valueOf(iPage.getTotal())
                .intValue(),page,pageSize,iPage.getRecords());
    }

    @Override
    public HouseResources queryHouseResourceById(Long id) {
        return super.queryById(id);
    }

    @Override
    public boolean updateResources(HouseResources houseResources) {
        System.out.println(houseResources.getId());
        return super.updateById(houseResources) == 1;
    }

}
